Introduction to Jupyter Notebooks on Navigating Complexity

On Navigating Complexity you keep "a lab notebook". It serves as your journal, notebook, sketchbook, diary, and more. Unlike finished products such as publications, notebooks are sketchy and very messy and they document the process as it is taking place. Science in the making. Your notes are your own, and freeform.

For note keeping, we use Jupyter Notebooks. There are five submissions during the course, on weeks 7, 10 and 13 you will submit your lab notebook. Also some of the course content, e.g. Thursday exercises and extra content are in this format.

Jupyter Notebooks are a document format and what makes them special, is that parts of the document are meant for humans, and parts for computers. For the latter, we use the Python programming language.

Notebooks are structured much like a text, running from beginning to the end. The prose can be interleaved with code, and computed output. It can be something very simple such as


In [1]:
len("in all honesty, counting all the words in a sentence is best done in a computers mind. It doesn't mind counting at all".split())


Out[1]:
23

Or how many unique words are there.


In [2]:
len(set("in all honesty, counting all the words in a sentence is best done in a computers mind. It doesn't mind counting at all".split()))


Out[2]:
17

Or something more complicated, such as defining how a graph is produced.


In [3]:
# Setup by importing some libraries and configuring a few things
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets
import IPython
%matplotlib inline

# Create two random datasets
data = np.random.random(15)
smallerdata = np.random.random(15) * 0.3

# Define a plotting function
def drawPlot():
    fig, ax = plt.subplots()
    ax.plot(range(len(data)), data, label="random data");
    ax.plot(range(len(smallerdata)), smallerdata, 'r--', label="smaller random data");
    plt.title("Two random dataset compared");
    ax.grid(axis='y');
    ax.legend(loc='upper right');
    return fig, ax

# Draw the plot
fig, ax = drawPlot()
plt.show()


Or even something interactive, such as


In [5]:
# Define an interactive plotting function
def updatePlot(s=0):
    print("data {0:.2f}, smallerdata {1:.2f}".format(data[s], smallerdata[s]))
    fig, ax = drawPlot()
    ax.axvline(s, color="grey", linestyle='dotted')
    ax.annotate(s=round(data[s], 2),
                 xy=(s, data[s]),
                 xytext=(s + 2, 0.5),
                 arrowprops={'arrowstyle': '->'});
    ax.annotate(s=round(smallerdata[s], 2),
                 xy=(s, smallerdata[s]),
                 xytext=(s + 2, 0.3),
                 arrowprops={'arrowstyle': '->'});
    plt.show();

# Define an interactive slider for exploring the data
slider = ipywidgets.interactive(updatePlot, s=(0, len(data) - 1, 1));
IPython.display.display(slider)


We don't do any programming on this course and use them simply for text editing, but we will get to see some software code. Jupyter Notebooks are perfect for that.

Required software

For NavCom, you will need the following software:

  • Python 3 programming language
  • Jupyter and the notebook extension
  • Python libraries Numpy, matplotlib, Pandas, NetworkX and ipywidgets

Install all of them on your computer at the beginning of the course. You can get Python 3 from https://python.org. After installing Python 3, install the others by running the following command at the command line, by first opening the command line (Terminal on OSX, and cmd on Windows):

pip3 install jupyter notebook numpy pandas networkx ipywidgets

Alternatively you can install Anaconda, which includes all of these things.

In addition you need git to download some of the Thursday assignments.

Necessary beginning skills

You can learn about Jupyter Notebooks best by watching a short introduction video or two on YouTube, and going through a tutorial. There are plenty online.

The essential components are:

  • Python kernel. It is your co-researcher, a machine to think with
  • Jupyter server. It is a web server on your machine and it coordinates all the things
  • Notebooks. They are the documents you edit

Make sure you learn the following concepts about Jupyter Notebooks:

  • cells
  • the two cell types: Markdown and code
  • and what is Markdown
  • edit and view modes
  • how to render Markdown cells and run code, by pressing Ctrl-Enter
  • how to start and stop the notebook server

Some of the Thursday exercises are distributed via GitHub. Get them on your computer by running the following command once on the command line

git clone https://github.com/ituethoslab/navcom-2017.git

And then to following commands on the command line once a week, at the beginning of the exercises, to receive updates

cd navcom-2017
git pull

And then, again on the command line

jupyter notebook

To start the notebook system. Feel free to edit your copies of the exercise notebooks.